#!/bin/tcsh -fe
####
#
#############################################################################
#                                                                           #
# Title: Init Program                                                       #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 09/01/2006                                             #
# Last Modification: 09/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 1
#
# MANUAL: <B>Welcome to 2dx_image</B>
#
# MANUAL: This program assists you in the processing of one 2D crystal image. Help is available <A HREF="http://2dx.org/documentation/2dx-software">here</A>. Try the right mouse click on the various fields or variable names, to get more information. See also the introductory <A HREF="http://2dx.org/documentation/2dx-software/tutorial"><B>online video tutorials</B></A>.
#
# MANUAL: 2dx_image uses external programs to display certain files. These can be defined in the 2dx configuration file, which you can find (and edit) in your home directory under .2dx/2dx.cfg . Use this file to define your prefered editor, web browser, etc..
#
# MANUAL: Edit the parameters in this central <I>Processing Data</I> pane. By default, only the most important parameters are accessible here. If you want to change one of the <I>advanced</I> parameters too, you can switch the display mode of this <I>Processing Data</I> pane at the left end of the top bar from "Simplified" to "Full Parameter List".
#
# MANUAL: 2dx tries to calculate or predict values for all parameters, based on available information. In several cases, you need to manually verify these values. These are then unerlaid with a blue stripe-pattern, until you have edited or confirmed manually the values.   
#
# MANUAL: After having edited the parameters, choose a script from the <I>Standard Scripts</I> and execute it via the <I>Start</I> button above.
#
# MANUAL: Upon first launch of this program, a default database in form of the file 2dx_image.cfg is created in the chosen  directory. The default entries for this database file are taken from the ${appDir_2dx}2dx/config/2dx_image.cfg file. These default values are then updated with values from the file ../2dx_image.cfg (the database file one level up from the current directory). Finally, this script 2dx_initialize.script is executed, and the resulting databse saved into the local directory's 2dx_image.cfg file.
#
# MANUAL: <HR>
#
# MANUAL: <IMG SRC="${appDir_2dx}/../resources/config/2dx_image-algo-1.jpg">
#
# MANUAL: <HR>
#
# MANUAL: <IMG SRC="${appDir_2dx}/../resources/config/2dx_image-algo-2.jpg">
#
# DISPLAY: imagename_original
# DISPLAY: imagename
# DISPLAY: image_flag
# DISPLAY: nonmaskimagename
# DISPLAY: movie_stackname
# DISPLAY: movie_stackname_raw
# DISPLAY: movie_enable
# DISPLAY: imagenumber
# DISPLAY: image_evenodd
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: Calc_from_sample_pixel
# DISPLAY: sample_pixel
# DISPLAY: realcell
# DISPLAY: realang
# DISPLAY: df_min
# DISPLAY: df_searchrange1
# DISPLAY: RESMAX
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: tempkeep
# DISPLAY: initialization_executable
# DISPLAY: initialization_corr_endian
# DISPLAY: det_defocus
# DISPLAY: det_tilt
# DISPLAY: crop
# DISPLAY: comment
# DISPLAY: imagesidelength
# DISPLAY: initialization_reset
# DISPLAY: use_masked_image
# DISPLAY: domask
# DISPLAY: ctfcor_imode
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
#
# Variables to be available from the config file are:
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set imagesidelength = ""
set realcell = ""
set image_evenodd = ""
set magnification = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set realang = ""
set phacon = ""
set RESMIN = ""
set RESMAX = ""
set RADLIM = ""
set det_tilt = ""
set boxa1 = ""
set boxa2 = ""
set boxb1 = ""
set boxb2 = ""
set quadrada = ""
set quadradb = ""
set initialization_reset = ""
set initialization_executable = ""
set initialization_corr_endian = ""
set crop = ""
set movie_inmovie = ""
set use_masked_image = ""
set backup_large_images = ""
set CS = ""
set KV = ""
#
#$end_vars
#
if ( ! -d LOGS ) then
  \mkdir LOGS
endif
#
set scriptname = 2dx_initialize
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
set IS_2DX = yes
source ${proc_2dx}/initialize
#
if ( ${movie_inmovie} == "n" ) then
  echo "<<@evaluate>>"
  echo "<<@progress: 1>>"
endif
#
if ( -e ${nonmaskimagename}.raw.mrc ) then
  \mv -f ${nonmaskimagename}.raw.mrc ${nonmaskimagename}_raw.mrc 
  echo "(Renaming ${nonmaskimagename}.raw.mrc to ${nonmaskimagename}_raw.mrc)"
endif
if ( -e ${nonmaskimagename}-masking-final.mrc ) then
  \mv -f ${nonmaskimagename}-masking-final.mrc ${nonmaskimagename}_mask.mrc 
  echo "(Renaming ${nonmaskimagename}-masking-final.mrc to ${nonmaskimagename}_mask.mrc)"
endif
if ( ! -e ${nonmaskimagename}_raw.mrc ) then
  if ( -e ${nonmaskimagename}.mrc ) then
    \mv -f ${nonmaskimagename}.mrc ${nonmaskimagename}_raw.mrc 
    echo "(Renaming ${nonmaskimagename}.mrc to ${nonmaskimagename}_raw.mrc)"
  endif
endif
#
set curdir = `pwd`
echo "::working directory = " ${curdir}
#
if ( ${initialization_executable} == "n" ) then
  set initialization_reset = "n"
endif
#
if ( ${initialization_reset} == "y" ) then
  echo "::Resetting image data"
  #
  # set reset_var = imagename          ; set reset_val = "ScriptWillPutNameHere" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  # set reset_var = nonmaskimagename   ; set reset_val = "ScriptWillPutNameHere" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  # set reset_var = imagenumber        ; set reset_val = "ScriptWillPutNumberHere" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = imagesidelength    ; set reset_val = "ScriptWillPutLengthHere" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  # set reset_var = defocus            ; set reset_val = "5000.0,5000.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  # set reset_var = defocusbackup      ; set reset_val = "5000.0,5000.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = realcell_local     ; set reset_val = "100.0,100.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = realang_local      ; set reset_val = "90.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = lattice            ; set reset_val = "0.0,0.0,0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = secondlattice      ; set reset_val = "0.0,0.0,0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = backuplattice      ; set reset_val = "0.0,0.0,0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = phaori             ; set reset_val = "0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = phaoriFourFilter   ; set reset_val = "0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = phaori_change      ; set reset_val = "0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = phaori_last_change ; set reset_val = "0.0,0.0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MergePhaseResidual ; set reset_val = "999.90" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = comment            ; set reset_val = "-"      ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_val = "."
  #
  set reset_var = JREFL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = JREFL_2D ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = Reflections_Unique ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = num_amplitudes_observed ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = num_phases_observed ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = num_reflections_fitted ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = num_reflections_FOM1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = num_reflections_FOM50 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_R_factor ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_phase_residual ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_weighted_R_factor ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_weighted_phase_residual ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_phase_residual_2D ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = overall_weighted_phase_residual_2D ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = PHARES_SYM ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = PHARES_NUM_SPOTS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = PHARES_Merge ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = QVAL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = QVAL1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = QVAL2 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = QVALS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = CALCULATEDMAG ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = U1_IQ1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ2 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ3 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ4 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ5 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ6 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ7 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ8 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U1_IQ9 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = U2_IQ1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ2 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ3 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ4 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ5 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ6 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ7 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ8 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = U2_IQ9 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = US_IQ1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ2 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ3 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ4 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ5 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ6 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ7 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ8 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = US_IQ9 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = RB_1 ; set reset_val = "15" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RB_2 ; set reset_val = "12" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RB_3 ; set reset_val = "9" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RB_4 ; set reset_val = "7" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RB_5 ; set reset_val = "5" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RB_6 ; set reset_val = "3" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_val = "."
  #
  set reset_var = RP_1 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RP_2 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RP_3 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RP_4 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RP_5 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = RP_6 ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_val = "--"
  set reset_var = DEFOCUS_TLTAXIS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = DEFOCUS_TLTANG ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = DEFOCUS_TLTAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = DEFOCUS_TAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = DEFOCUS_TANGL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = LATTICE_TLTAXIS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = LATTICE_TLTANG ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = LATTICE_TLTAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = LATTICE_TAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = LATTICE_TANGL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = TTREFINE_TLTAXIS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = TTREFINE_TLTANG ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = TTREFINE_TLTAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = TTREFINE_TAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = TTREFINE_TANGL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  set reset_var = MERGE_TLTAXIS ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MERGE_TLTANG ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MERGE_TLTAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MERGE_TAXA ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MERGE_TANGL ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  #
  set reset_var = DEFOCUS_ACTIVE ; set reset_val = "0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = FFT_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = DEFOCUS_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = LATTICE_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = SPOTS_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = UNBENDING_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = ML_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = CTF_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MASKING_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = PHASEORI_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = MERGING_done ; set reset_val = "n" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = REPROCESSING_done ; set reset_val = "n"  ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  set reset_var = dummy ; set reset_val = "0" ; source ${proc_2dx}/2dx_initialize_reset_sub.com
  #
  #
  #
  set initialization_reset = "n"
  echo "set initialization_reset = n" >> LOGS/${scriptname}.results
endif
#
if ( ${CS} == "ScriptWillPutNumberHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumberHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif
#
echo "# IMAGE-IMPORTANT: ${nonmaskimagename}_raw.mrc <RAW Non-Masked Image>" >> LOGS/${scriptname}.results
if ( ${nonmaskimagename} != ${imagename} ) then
  echo "# IMAGE-IMPORTANT: ${imagename}.mrc <Image>"  >> LOGS/${scriptname}.results
endif
echo "# IMAGE-IMPORTANT: ${nonmaskimagename}.mrc <Non-Masked Image>" >> LOGS/${scriptname}.results
#
#############################################################################
echo ":: "
echo "::                           ************************"
echo "::                             Welcome to 2dx_image"
echo "::                           ************************"
echo "::                (activate the MANUAL button top right for help)"
echo ":: "
#############################################################################
#
if ( ${movie_inmovie} == "n" ) then
  echo "<<@progress: 100>>"
endif
#
exit
#
# This here is just to make sure the script is displayed in the GUI:
source ${proc_2dx}/2dx_initialize_reset_sub.com
source ${proc_2dx}/2dx_makedirs
#
